package com.power.sifa.correction.service.impl;

import cn.hutool.core.lang.id.NanoId;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.model.Crw_rule_group;
import com.power.sifa.correction.model.Crw_rule_night;
import com.power.sifa.correction.model.Crw_rule_person;
import com.power.sifa.correction.service.Crw_rule_groupService;
import com.power.sifa.correction.service.Crw_rule_personService;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.correction.model.Crw_rule_samecase;
import com.power.sifa.correction.dao.Crw_rule_samecaseDao;
import com.power.sifa.correction.service.Crw_rule_samecaseService;
import com.power.sifa.correction.model.pageInfo.Crw_rule_samecasePageInfo;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *  同案类案人设置业务层实现
 */
@Service
public class Crw_rule_samecaseServiceImpl extends BaseServiceImpl implements Crw_rule_samecaseService {

    @Autowired
    private Crw_rule_samecaseDao dao;

    @Resource
    private Crw_rule_personService crw_rule_personService;

    @Resource
    private Crw_rule_groupService crw_rule_groupService;

    @Override
    public Crw_rule_samecase getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Crw_rule_samecase crw_rule_samecase) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (crw_rule_samecase.getPrimaryKey() != null) {
        	 Crw_rule_samecase crw_rule_samecasefDb = dao.findById(crw_rule_samecase.getPrimaryKey()).orElse(null);
             if(crw_rule_samecasefDb != null) {
                BeanUtils.copyProperties(crw_rule_samecase, crw_rule_samecasefDb);
                dao.save(crw_rule_samecasefDb);
                return;
             }
        }
        dao.save(crw_rule_samecase);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Crw_rule_samecase> findAll(Crw_rule_samecasePageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<String> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Crw_rule_samecase> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Crw_rule_samecase> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Crw_rule_samecase> findByRuleIds(List<String> ruleIds) {
        return dao.findByRuleIds(ruleIds);
    }

    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRule(Crw_rule_samecase crw_rule_samecase, List<String> personIds, List<String> groupIds) {

        if (Objects.isNull(crw_rule_samecase.getId())) {
            String ruleId = "D" + NanoId.randomNanoId();
            crw_rule_samecase.setId(ruleId);
            dao.save(crw_rule_samecase);

            crw_rule_personService.saveAll(ruleId, personIds);

            crw_rule_groupService.saveAll(ruleId, groupIds);

        } else {
            dao.save(crw_rule_samecase);
            String ruleId = crw_rule_samecase.getId();

            crw_rule_personService.removeByRuleId(ruleId);
            crw_rule_personService.saveAll(ruleId, personIds);

            crw_rule_groupService.removeByRuleId(ruleId);
            crw_rule_groupService.saveAll(ruleId, groupIds);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeRule(String id) {
        dao.deleteById(id);

        crw_rule_personService.removeByRuleId(id);

        crw_rule_groupService.removeByRuleId(id);
    }

    @Override
    public void removeRules(List<String> list) {
        dao.removeByIds(list);

        crw_rule_personService.removeByRuleIds(list);

        crw_rule_groupService.removeByRuleIds(list);
    }

    @Override
    public Boolean existsRuleName(String name) {
        return CollectionUtils.isNotEmpty(dao.findByName(name));
    }
}
